package com.ruoyi.web.controller.consumerAdmin;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.domain.COrderInvoice;
import com.ruoyi.consumer.service.impl.CCompanyServiceImpl;
import com.ruoyi.consumer.service.impl.COrderInvoiceServiceImpl;
import com.ruoyi.consumer.utils.TableDataUtil;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.utils.IpAddressUtils.getAddressInfo;

/**
 * @author: lizhouwei
 * @ date : 2023/11/14
 * @description:
 */
@Api(tags = "发票相关操作")
@RestController
@RequestMapping("/api/invoice")
public class InvoiceController {



    @Autowired
    private CCompanyServiceImpl companyService;

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Autowired
    private COrderInvoiceServiceImpl invoiceService;


    //公司用户申请开票
    @ApiOperation("公司用户申请开票")
    @PostMapping("/apply-invoice")
    public AjaxResult applyInvoice(@RequestBody COrderInvoice invoice) {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        if (sysUser == null) {
            new RuntimeException("管理员不存在");
        }
        //查询当前管理员所处公司
        CCompany company = new CCompany();
        company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
        List<CCompany> cCompanies = companyService.selectCCompanyList(company);
        if (cCompanies.size() == 0) {
            new RuntimeException("管理员所处公司不存在");
        }
        invoice.setCompanyId(cCompanies.get(0).getCompanyId());
        return AjaxResult.success(invoiceService.insertCOrderInvoice(invoice));
    }
    //公司用户查看开票记录
    @ApiOperation("公司用户查看开票记录")
    @GetMapping("/list-invoice")
    public TableDataInfo listInvoice(COrderInvoice invoice) {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        if (sysUser == null) {
             new RuntimeException("管理员不存在");
        }
        //查询当前管理员所处公司
        CCompany company = new CCompany();
        company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
        List<CCompany> cCompanies = companyService.selectCCompanyList(company);
        if (cCompanies.size() == 0) {
            new RuntimeException("管理员所处公司不存在");
        }

        invoice.setCompanyId(cCompanies.get(0).getCompanyId());
        List<COrderInvoice> cOrderInvoices = invoiceService.selectCOrderInvoiceList(invoice);
        return TableDataUtil.getDataTable(cOrderInvoices);
    }

    /**
     * 本地资源通用下载
     */
    @ApiOperation("/文件下载")
    @GetMapping("/common/download/resource")
    public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        // 本地资源路径
        String localPath = getAddressInfo();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

    //查看某个订单是否已经开票
    @ApiOperation("查看某个订单是否已经开票")
    @GetMapping("/is-invoice/{orderId}")
    public AjaxResult isInvoice(@ApiParam("订单id") @PathVariable("orderId") Integer orderId) {
        COrderInvoice invoice = new COrderInvoice();
        invoice.setOrderId(orderId);
        List<COrderInvoice> cOrderInvoices = invoiceService.selectCOrderInvoiceList(invoice);
        if (cOrderInvoices.size() == 0) {
            return AjaxResult.success(false);
        }
        if (cOrderInvoices.get(0).getStatus() == 0 || cOrderInvoices.get(0).getStatus() == 1) {
            return AjaxResult.success(true);
        }
        return AjaxResult.success(false);
    }


}
